<?php
namespace tool;

class OutPut {

    //public static $_ERROR_NO_ERROR = array('CODE' => 0);
    public static $_ERROR_AUTHORIZATION_FAIL = ['CODE' => 1001, 'MESSAGE' => '无权限访问', 'SYS_MESSAGE' => ''];
    public static $_ERROR_REJECT_REPEAT_REQ =  ['CODE' => 1002, 'MESSAGE' => '请求太快，请稍候重试'];
    public static $_ERROR_PARAMS_ERROR =  ['CODE' => 1003, 'MESSAGE' => '参数错误', 'SYS_MESSAGE' => ''];
    //public static $_ERROR_SIGN_FAIL =  ['CODE' => 1004, 'MESSAGE' => '签名错误'];
    //public static $_ERROR_NONCE_REPEAT =  ['CODE' => 1005, 'MESSAGE' => 'nonce无效'];
    //public static $_ERROR_REQUEST_METHOD =  ['CODE' => 1006, 'MESSAGE' => '请求方式有误'];
    public static $_ERROR_VIDEO_RENDER_FAIL =  ['CODE' => 3001, 'MESSAGE' => '视频渲染失败', 'SYS_MESSAGE'=>''];
    public static $_ERROR_SHARE_FAIL =  ['CODE' => 3002, 'MESSAGE' => '分享失败', 'SYS_MESSAGE'=>''];


    /**
     * 返回信息
     * @param array $arr 接口返回数组
     */
    public static function success($data, $nonObject = false)
    {
        if ($nonObject == false) {
            return json($data);
        } else {
            return json_encode($data, JSON_UNESCAPED_UNICODE);
        }
    }

    /**
     * 返回错误信息
     * @param array $errorCode 错误码，以 $_ERROR_ 开头
     * @param array $errorArr 附加错误串，可选
     */
    public static function error($errorCode, $errorArr = array(), $nonObject = false)
    {

        if (!is_array($errorCode))
            $errorCode = self::$_ERROR_ERROR_WRONG_ERROR_CODE;
        if (!is_array($errorArr)) {
            $errorArr = array('errorcode' => $errorCode, 'errordesc' => serialize($errorArr));
            $errorCode = self::$_ERROR_ERROR_WRONG_ERROR_ARR;
        }

        if ($nonObject == false) {
            return json([
                'errorcode'     => $errorCode['CODE'],
                'errordesc'     => $errorCode['MESSAGE'],
                'errorsysdesc'  => $errorCode['SYS_MESSAGE'],
                'errorspecific' => $errorArr,
            ]);
        } else {
            return json_encode([
                'errorcode'     => $errorCode['CODE'],
                'errordesc'     => $errorCode['MESSAGE'],
                'errorsysdesc'  => $errorCode['SYS_MESSAGE'],
                'errorspecific' => $errorArr,
            ], JSON_UNESCAPED_UNICODE);
        }
    }

    /**
     * 返回自定义错误信息
     * @param String $errorDesc 错误信息
     * @param array $errorArr 附加错误串，可选
     */
    public static function customError($errorDesc, $errorArr = array(), $nonObject = false)
    {

        $errorCode = self::$_ERROR_OTHER_CUSTOM_ERROR;
        if ($errorDesc)
            $errorCode['MESSAGE'] = $errorDesc;
        if (!is_array($errorArr))
            $errorArr = (array)$errorArr;

        if ($nonObject == false) {
            return json([
                'errorcode'     => $errorCode['CODE'],
                'errordesc'     => $errorCode['MESSAGE'],
                'errorsysdesc'  => $errorCode['SYS_MESSAGE'],
                'errorspecific' => $errorArr,
                'data'          => ''
            ]);
        } else {
            return json_encode([
                'errorcode'     => $errorCode['CODE'],
                'errordesc'     => $errorCode['MESSAGE'],
                'errorsysdesc'  => $errorCode['SYS_MESSAGE'],
                'errorspecific' => $errorArr,
            ], JSON_UNESCAPED_UNICODE);
        }
    }
}